<template>
  <el-dialog
    ref="selectFlow2"
    :title="params.title || '选择流程'"
    width="400px"
    :visible.sync="params.visible"
  >
    <compTree
      ref="tree"
      :show-search="false"
      :tree-show-checkbox="params.ismulti"
      :tree-default-expanded-keys="treeDefaultExpandedKeys"
      :comp-tree-height="350"
      :tree-data="treeData"
      :tree-default-checked-keys="treeDefaultCheckedKeys"
      :is-unselect="true"
    />
    <dialog-btn slot="footer" />
  </el-dialog>
</template>

<script>
import { getRpaFlowList } from '@/api/flow'
import { ParamArr, SQLTable } from '../../../public/js/paramarr'
import Global from '../../../public/js/global'
export default {
  props: {
    params: {
      type: Object,
      default() {
        return {
          visible: false,
          id: '', // 流程id
          ismulti: false, // 是否多选
          isNullSelect: false // 是否可不选
        }
      }
    }
  },
  data() {
    return {
      treeData: [],
      treeDefaultCheckedKeys: [],
      treeDefaultExpandedKeys: []
    }
  },
  created() {
    this.initWindow()
  },
  methods: {
    confirm() {
      const tree = this.$refs.tree
      if (this.params.ismulti) {
        const nodes = tree.getCheckedNodes().filter((item) => {
          return !item.folder
        })
        if (!this.params.isNullSelect && this.Global.isEmptyObject(nodes)) {
          return this.$message.warning('请勾选流程')
        }
        const ids = []
        const names = []
        for (let i = 0; i < nodes.length; i++) {
          ids.push(nodes[i].id)
          names.push(nodes[i].name)
        }
        this.params.confirm({
          id: ids.join(','),
          name: names.join(',')
        })
      } else {
        const node = tree.getCurrentNode()
        if (!this.params.isNullSelect && (!node || node.folder)) {
          return this.$message.warning('请选择流程')
        }
        this.params.confirm({
          id: node.id,
          name: node.name
        })
      }
    },
    initWindow() {
      getRpaFlowList(new ParamArr()).then((vPar) => {
        const nodes = []
        const nodeMap = new Map()
        let table = new SQLTable(vPar.getTable('k_flow_group'))
        table.first()
        while (!table.eof()) {
          const pnode = Global.getTreeviewPNode(
            nodeMap,
            nodes,
            table.getValueByName('PID')
          )
          const node = {}
          node['id'] = table.getValueByName('ID')
          node['name'] = table.getValueByName('Name')
          node['text'] = node['name']
          node['icon'] = 'wenjianyi'
          node['type'] = 0
          node['folder'] = true
          pnode.push(node)
          nodeMap.set(node['id'], node)
          table.next()
        }
        table = new SQLTable(vPar.getTable('k_flow_info'))
        table.first()
        while (!table.eof()) {
          const pnode = Global.getTreeviewPNode(
            nodeMap,
            nodes,
            table.getValueByName('PID')
          )
          const node = {}
          node['id'] = table.getValueByName('FlowID')
          node['name'] = table.getValueByName('FlowName')
          node['text'] = node['name']
          node['icon'] = 'liuchen'
          node['type'] = 1
          pnode.push(node)
          nodeMap.set(node['id'], node)
          table.next()
        }

        if (this.params.id) {
          // 多选回显
          if (this.params.ismulti) {
            const arr = this.params.id.split(',')
            this.treeDefaultCheckedKeys = arr
            this.treeDefaultExpandedKeys = arr
          } else {
            // 单选回显
            this.treeDefaultExpandedKeys.push(this.params.id)
            this.$nextTick(() => {
              this.$refs.tree.setCurrentKey(this.params.id)
            })
          }
        }

        this.treeData = nodes
      })
    }
  }
}
</script>

<style scoped>
</style>
